package org.example.smart.Client;

import com.briup.smart.env.client.Gather;
import com.briup.smart.env.entity.Environment;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class GatherImpl implements Gather {

    @Override
    public Collection<Environment> gather(){
        File file = new File("D:\\JDBC_project\\JDBC_Demo\\src\\main\\resources\\data-file-simple");
        List<Environment> list = new ArrayList<>();
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file));){
            while (bufferedReader.ready()){
                Environment environment = new Environment();
                String line = bufferedReader.readLine();
                String[] arr = line.split("\\|");
                environment.setSrcId(arr[0]);
                environment.setDesId(arr[1]);
                environment.setDevId(arr[2]);
                environment.setSensorAddress(arr[3]);
                environment.setCount(Integer.parseInt(arr[4]));
                //3表示结束数据，16表示发送命令
                environment.setCmd(arr[5]);
                //环境数据表值,在下面的计算中
                //environment.setData(Float.parseFloat(arr[6]));
                environment.setStatus(Integer.parseInt(arr[7]));
                environment.setGatherDate(new Timestamp(Long.parseLong(arr[8])));
                switch (arr[3]){
                    case "16":
                        String temp = arr[6].substring(0,4);
                        int temp_int = Integer.parseInt(temp,16);
                        float temp_real = (temp_int*(0.00268127F))-46.85F;
                        environment.setData(temp_real);
                        environment.setName("温度");
                        //一个对象完成，放在集合中
                        list.add(environment);
                        Environment environment2 = new Environment("湿度",environment.getSrcId(),environment.getDesId(),environment.getDevId(),environment.getSensorAddress(),environment.getCount(),environment.getCmd(),environment.getStatus(),0,environment.getGatherDate());
                        String humi = arr[6].substring(4,8);
                        int humi_int = Integer.parseInt(humi,16);
                        float humi_real = (humi_int*0.00190735F)-6;
                        environment2.setData(humi_real);
                        list.add(environment2);
                        break;
                    case "256":
                        String light = arr[6].substring(0,4);
                        int light_int = Integer.parseInt(light,16);
                        environment.setData(light_int);
                        environment.setName("光照强度");
                        list.add(environment);
                        break;
                    case "1280":
                        String co2 = arr[6].substring(0,4);
                        int co2_int = Integer.parseInt(co2,16);
                        environment.setData(co2_int);
                        environment.setName("二氧化碳浓度");
                        list.add(environment);
                        break;
                    default:
//                    System.out.println("数据格式有误，请排查");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("采集到的对象的个数" + list.size());
        return list;
    }
}
